package jmu.edu.yjc.controller;

import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Menu;
import jmu.edu.yjc.domain.vo.menu.MenuVo;
import jmu.edu.yjc.service.MenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/menu")
public class MenuController {
    @Resource
    private MenuService menuService;
    @GetMapping("/list")
    public ResponseResult menuList(String menuName , String status){
        return menuService.menuList(menuName,status);
    }
    @PostMapping
    public ResponseResult addMenu(@RequestBody Menu menu){
        menuService.save(menu);
        return ResponseResult.okResult();
    }
    @GetMapping("/{menuId}")
    public ResponseResult editMenu(@PathVariable("menuId") Long menuId){
        Menu menu = menuService.getById(menuId);
        return ResponseResult.okResult(BeanCopyUtil.copyBeans(menu, MenuVo.class));
    }
    @PutMapping
    public ResponseResult doEditMenu(@RequestBody Menu menu){
        if (menu.getId().equals(menu.getParentId())) {
            return ResponseResult.errorResult(500,"修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menuService.updateById(menu);
        return ResponseResult.okResult();
    }
    @DeleteMapping("/{menuId}")
    public ResponseResult deleteMenu(@PathVariable("menuId") Long menuId){
        if (menuService.hasChild(menuId)) {
            return ResponseResult.errorResult(500,"存在子菜单不允许删除");
        }
        menuService.removeById(menuId);
        return ResponseResult.okResult();
    }
    @GetMapping("/treeselect")
    public ResponseResult treeselect(){
        return menuService.treeselect();
    }
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public ResponseResult roleMenuTreeselect(@PathVariable("roleId") Long roleId){
        return menuService.roleMenuTreeselect(roleId);
    }
}
